package com.tongfusoft.document.doc.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.tongfuosft.document.system.service.IDocSystempParamService;
import com.tongfusoft.document.doc.service.IFileStroageService;
import com.tongfusoft.document.entity.DocHistory;
import com.tongfusoft.document.entity.Docs;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.utils.DateUtils;
import com.tongfusoft.utils.FileUtils;
import com.tongfusoft.utils.UIDGeneratorService;

@Service("fileStroageService")
public class FileStroageServiceImpl implements IFileStroageService {
	@Autowired
	@Qualifier("docSystemParamService")
	public IDocSystempParamService systemParamService;

	@Override
	public String stroage(Docs doc,InputStream in, String fileName) {
		try {
			String filePath = FileUtils
					.writeFile(in, this.systemParamService.getParamByCode("doc.file.path").getSystemValue() + File.separator + DateUtils.format(new Date(), "yyyyMMdd"),
							UIDGeneratorService.getUUID() + "." + FileUtils.getExtensionName(fileName))
					.getPath();
			return filePath;
		} catch (IOException e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	@Override
	public InputStream read(DocHistory doc) {
		
		try {
			return new FileInputStream(new File(doc.getDocPath()));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	

}
